<?php

namespace falcon5\inc\Infrastructure\Logger;

use falcon5\inc\Base\Common;
use hollisho\helpers\ArrayHelper;
use Monolog\Formatter\JsonFormatter;

class LogJsonFormatter extends JsonFormatter
{
    public function format(array $record): string
    {
        $level = ArrayHelper::getValue($record, 'level_name', 'info');
        $channel = ArrayHelper::getValue($record, 'channel');
        $message = ArrayHelper::getValue($record, 'message', '');
        $context = ArrayHelper::getValue($record, 'context', []);
        $extra = ArrayHelper::getValue($record, 'extra', []);
        $datetime = ArrayHelper::getValue($record, 'datetime');
        $datetime = $datetime->format('Y-m-d H:i:s');

        $trace_id = $record['extra']['uid'] ?? '';
        $uri = $record['extra']['url'] ?? '';
        return $this->toJson($this->normalize([
                'datetime' => $datetime,
                'msg' => $message,
                'level' => strtolower($level),
                'module' => Common::PLUGIN_ID,
                'request_params' => '',
                'trace_id' => $trace_id,
                'uri' => $uri,
                'context' => json_encode($context),
                'extra' => json_encode($extra),
            ]), true) . ($this->appendNewline ? "\n" : '');

    }
}